package com.hotel.api.service.impl;

import com.hotel.api.common.Common;
import com.hotel.api.entity.Suggestion;
import com.hotel.api.entity.User;
import com.hotel.api.mapper.SuggestionMapper;
import com.hotel.api.mapper.UserMapper;
import com.hotel.api.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.sound.midi.Soundbank;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * TODO
 *
 * @Author 涛涛
 * @Create 2019/8/29
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private SuggestionMapper suggestionMapper;
    @Autowired
    private UserMapper userMapper;

    /**
     * 添加意见信息
     *
     * @param suggestion
     * @return
     */
    @Override
    public int addSuggestion(Suggestion suggestion) {
        return suggestionMapper.insert(suggestion);
    }

    /**
     * 根据uid查询个人意见信息
     *
     * @param uid
     * @return
     */
    @Override
    public List<Suggestion> findByUid(int uid) {
        return suggestionMapper.selectByUid(uid);
    }

    /**
     * 查询所有意见信息
     *
     * @return
     */
    @Override
    public List<Suggestion> findAll() {
        return suggestionMapper.selectAll();
    }

    /**
     * 根据状态state查询意见信息
     *
     * @param state
     * @return
     */
    @Override
    public List<Suggestion> findAllByState(int state) {
        return suggestionMapper.selectAllByState(state);
    }

    /**
     * 更新意见信息，0为管理员，1为工作人员，2为用户
     *
     * @param suggestion
     * @return
     */
    @Override
    public int updateSuggestion(Suggestion suggestion) {
       return suggestionMapper.update(suggestion);
    }


    @Override
    public int update(User record) {
        SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String date = sd.format(new Date());
        record.setUpdateTime(date);
        return userMapper.update(record);
    }

    /**
     * 登录，根据手机号进行登录
     *
     * @param phoneNumber
     * @return
     */
    @Override
    public User findByUsername(String phoneNumber) {
        return userMapper.findByUsername(phoneNumber);
    }

    /**
     * 根据id查询用户
     *
     * @param id
     * @return
     */
    @Override
    public User selectById(Integer id) {
        return userMapper.selectById(id);
    }

    /**
     * 添加用户
     *
     * @param record
     * @return
     */
    @Override
    public int insert(User record) {
        SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String date = sd.format(new Date());
        record.setCreateTime(date);
        record.setDelState((byte) 0);
        return userMapper.insert(record);
    }

    /**
     * 查询所有用户
     *
     * @return
     */
    @Override
    public List<User> selectAllUser() {
        return userMapper.selectAll();
    }

    /**
     * 根据ID查询用户
     *
     * @param rid
     * @return
     */
    @Override
    public List<User> selectByRoomId(Integer rid) {
        return userMapper.selectByRoomId(rid);
    }

    /**
     * 删除用户
     *
     * @param id
     * @return
     */
    @Override
    public int deleteUser(Integer id) {
        return userMapper.deleteUser(id);
    }

    /**
     * 根据名字查询用户
     *
     * @param name
     * @return
     */
    @Override
    public User selectByName(String name) {
        return userMapper.selectByName(name);
    }

    /**
     * 根据电话号码查询用户
     *
     * @param phoneNumber
     * @return
     */
    @Override
    public User selectByPhoneNumber(String phoneNumber) {
        return userMapper.selectByPhoneNumber(phoneNumber);
    }

    /**
     * 根据身份证号查询用户
     *
     * @param idcard
     * @return
     */
    @Override
    public User selectByIdcard(String idcard) {
        return userMapper.selectByIdcard(idcard);
    }

    /**
     * 根据公司名查询用户
     *
     * @param company
     * @return
     */
    @Override
    public User selectByCompany(String company) {
        return userMapper.selectByCompany(company);
    }

    /**
     * 退宿
     *
     * @param leaveTime
     * @param id
     * @param cause
     * @return
     */
    @Override
    public int leaveRoom(Integer id, String leaveTime, String cause) {
        int i = 0;
        User user = userMapper.selectById(id);
        if (user != null && user.getLeaveTime() == null) {
            user.setLeaveTime(leaveTime);
            user.setLeaveCause(cause);
            i = update(user);
            return i;
        }
        return i;
    }

    @Override
    public User selectByUserName(String phoneNumber) {
        return userMapper.selectByUserName(phoneNumber);
    }

    @Override
    public int modifyPasswordByUserId(User user) {
        return userMapper.update(user);
    }
}
